package jdbcConnect;

import java.io.*;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Student extends User {
    Scanner s = new Scanner(System.in);
    String loginuser;
    String loginpwd;
    Connection conn = new JDBC().JDBCConnectvoid();
    Statement stmt = conn.createStatement();
    ResultSet rs = null;
    private int MessageLenth;

    public void setMessageLenth(int messageLenth) {
        MessageLenth = messageLenth;
    }

    public int getMessageLenth() {
        return MessageLenth;
    }

    public Student() throws SQLException {

    }

    public Student(String user, String pwd) throws SQLException {
        this.loginuser = user;
        this.loginpwd = pwd;
    }

    @Override
    public void Select() throws SQLException {
        String sql = "select * from student where StudentNo='" + loginuser + "'";
        rs = stmt.executeQuery(sql);
        System.out.println("以下是关于您的信息：");
        while (rs.next()) {
            System.out.println(rs.getString(1) + "->" + rs.getString(2) + "->" + rs.getString(3) + "->" + rs.getString(4) + "->" + rs.getString(5));
        }
    }

    @Override
    public void Update() throws SQLException {
        Scanner s = new Scanner(System.in);
        String sql = "select * from student where StudentNo='" + loginuser + "'";
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) + "->" + rs.getString(2) + "->" + rs.getString(3) + "->" + rs.getString(4) + "->" + rs.getString(5));
        }
        System.out.println("请对该学生信息进行修改：");
        System.out.println("请依次输入该学生的姓名、年龄、性别、密码！");
        String TUpStuName = s.next();
        String TUpStuAge = s.next();
        String TUpStuSex = s.next();
        String TUpStuPwd = s.next();

        String UpStsql = "update student set StudentName='" + TUpStuName + "',age='" + TUpStuAge + "',sex='" + TUpStuSex + "',password='" + TUpStuPwd + "' where StudentNo='" + loginuser + "';";
        int Num = stmt.executeUpdate(UpStsql);
        if (Num != 0) System.out.println("修改成功！");
        else System.out.println("修改失败！");
    }

    public void SendMessage() throws IOException {
        System.out.println("请输入目标IP地址：");
        String IpAddress = s.next();
        //清除java缓冲区空格
        System.out.println("您当前消息的发送地址为:" + IpAddress);
        System.out.println("发送方启动中...");
        Socket socket = null;
        socket = new Socket(IpAddress, 1087);
        //获取输出流，写入数据
        OutputStream out = socket.getOutputStream();
        //将字节流包装成打印流
        PrintStream ps = new PrintStream(out);
        System.out.println("请输入您发送弹幕的数量:");
        int lenth = s.nextInt();
        boolean Tflag = true;
        do {
            if (lenth <= 0 || lenth > 100) {
                System.out.println("由于您输入的数量不符合规范");
                System.out.println("故客户端即将退出...");
                break;
            }
            int count=0;
            for (int j = 0; j < lenth; j++) {
                System.out.println("请输入您需要发送的消息：");
                String message = s.next();
                ps.println(message);
                ps.flush();
                count++;
                if(count==lenth) {
                    System.out.println("已超出输入的数量");
                    System.out.println("即将关闭客户端");
                    out.close();
                    socket.close();
                    System.out.println("客户端已关闭");
                    Tflag=false;
                    break;
                }
                System.out.println("是否继续发送？");
                System.out.println("1.继续发送\t2.退出到主界面");
                int answer = s.nextInt();
                if (answer != 1) {
                    System.out.println("即将关闭客户端");
                    out.close();
                    socket.close();
                    System.out.println("客户端已关闭");
                    break;
                }
            }
            Tflag=false;
        } while (Tflag);
    }

    public void OutTxt() throws IOException, SQLException {
        //如果一上来登陆的就是学生，那么就直接获取的就是输入端的学生用户名（学号）
        File file = new File("D:/IDEA/project/JDBCProject/" + File.separator + "学生信息.txt");
        OutputStream stream = new FileOutputStream(file);
        System.out.println("请输入您需要导出的学生学号:");
        String SStu_sql = "Select * from student where StudentNo='" + loginuser + "'";
        rs = stmt.executeQuery(SStu_sql);
        OutputStreamWriter writer = new OutputStreamWriter(stream);
        while (rs.next()) {
            writer.write(rs.getString(1) + "\t");
            writer.write(rs.getString(2) + "\t");
            writer.write(String.valueOf(rs.getInt(3)) + "\t");
            writer.write(rs.getString(4) + "\t");
            writer.write(rs.getString(5) + "\t");
            writer.write("\r\n");
        }
        System.out.println("导出成功！");
        //释放资源
        writer.flush();
        writer.close();
    }

}

